package model;

import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import java.util.EnumMap;
import javax.imageio.ImageIO;

/**
 * De klasse Field is verantwoordelijk voor: zichzelf TEKENNEN een Part kunnen
 * BEVATTEN
 *
 * Game heeft kennis van de Fields om hem heen en het Part wat er in hem zit
 *
 * Author Misja, Bob.
 */
public class Field {

    public enum Directions {

        RIGHT, LEFT, UP, DOWN
    }
    private int xCoordinate, yCoordinate;
    private EnumMap<Directions, Field> neighbors;
    private Part part;
    private Image terrain;

    public Field(int x, int y) {

        this.xCoordinate = x;
        this.yCoordinate = y;

        this.neighbors = new EnumMap<>(Directions.class);

        URL url = this.getClass().getResource("terrainlevel1.gif");
        try {
            this.terrain = ImageIO.read(url);
        } catch (IOException ex) {
        }
    }

    /**
     * Tekent het veld of roept de teken methode van het onderliggende object
     * aan
     *
     */
    public void draw(Graphics g) {
        if (this.part != null) {
            this.part.draw(g);
        } else {
            g.drawImage(terrain, this.xCoordinate, this.yCoordinate, null);
        }
    }

    /**
     * @return the xCoordinate
     */
    public int getxCoordinate() {
        return xCoordinate;
    }

    /**
     * @param xCoordinate the xCoordinate to set
     */
    public void setxCoordinate(int xCoordinate) {
        this.xCoordinate = xCoordinate;
    }

    /**
     * @return the yCoordinate
     */
    public int getyCoordinate() {
        return yCoordinate;
    }

    /**
     * @param yCoordinate the yCoordinate to set
     */
    public void setyCoordinate(int yCoordinate) {
        this.yCoordinate = yCoordinate;
    }

    /**
     * @return the part
     */
    public Part getPart() {
        return part;
    }

    /**
     * @param part the part to set
     */
    public void setPart(Part part) {
        this.part = part;
        if (part != null) {
            if (part.getCurrentField() == null) {
                part.setCurrentField(this);
            } else if (!part.getCurrentField().equals(this)) {
                part.setCurrentField(this);
            }
        }
    }

    /*
     * Opslaan van een buur
     */
    public void setNeighbor(Field neighbor, String direction) {
        switch (direction) {
            case "UP":
                this.neighbors.put(Directions.UP, neighbor);
                break;
            case "DOWN":
                this.neighbors.put(Directions.DOWN, neighbor);
                break;
            case "LEFT":
                this.neighbors.put(Directions.LEFT, neighbor);
                break;
            case "RIGHT":
                this.neighbors.put(Directions.RIGHT, neighbor);
                break;
        }
    }

    /*
     * Opvragen van een buur
     */
    public Field getNeighbor(String direction) {
        switch (direction) {
            case "UP":
                return this.neighbors.get(Directions.UP);
            case "DOWN":
                return this.neighbors.get(Directions.DOWN);
            case "LEFT":
                return this.neighbors.get(Directions.LEFT);
            case "RIGHT":
                return this.neighbors.get(Directions.RIGHT);
        }
        return null;
    }
}
